<?php

namespace common\components\fs;

use common\components\wtools\tools\FileHelper;
use common\components\wtools\tools\Model;
use common\models\db\Files;
use common\Tools;
use finfo;
use yii\base\Component;

class LocalFs extends Component implements FsApi
{
    public function uploadByTmpFile($tmp)
    {
        $pathInfo = pathinfo($tmp['name']);
        $ext = $pathInfo['extension'];
        $tmpFile = $tmp['tmp_name'];
        $content = file_get_contents($tmpFile);
        $md5 = md5($content);
        $size = filesize($tmpFile);
        $id_file_name = $md5.".".$size.".".$ext;
        $file = Files::findOne(['filename' => $id_file_name]);
        if (!$file) {
            $prefix = '/files/'.date('Ymd/');
            $yiiAliasUploadsRoot = '@uploads_root';
            $filePath = \Yii::getAlias($yiiAliasUploadsRoot).$prefix.$id_file_name;
            if (!is_dir(dirname($filePath))) {
                FileHelper::createDirectory(dirname($filePath));
            }
            move_uploaded_file($tmpFile, $filePath);
            $file = new Files();
            $file->filename = $id_file_name;
            $file->extension = $ext;
            $file->mime_type = \Yii::$app->wMimeTypes->getMimeType($ext);
            $file->relation_path = $prefix;
            $file->yii_alias_uploads_root = $yiiAliasUploadsRoot;
            $file->size = $size;
            $file->type = $file::TYPE_1;
            $file->component_id = 'localFs';
            if (!$file->save()) {
                throw new \Exception("文件保存失败：".Model::getModelError($file));
            }
        }
        return $file->abUrl;
    }

    public function uploadByBase64($base64)
    {
        $content = base64_decode($base64);
        // 创建finfo实例，用于获取文件MIME类型
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        // 获取MIME类型
        $mime_type = $finfo->buffer($content);
        $ext = \Yii::$app->wMimeTypes->getExtension($mime_type);
        $imgBase64 = "data:$mime_type;base64,$base64";
        $md5 = md5($base64);
        $size = Tools::getBase64ImgInfo($imgBase64, 'size');
        $id_file_name = $md5.".".$size.".".$ext;
        $file = Files::findOne(['filename' => $id_file_name]);
        if (!$file) {
            $prefix = '/files/'.date('Ymd/');
            $yiiAliasUploadsRoot = '@uploads_root';
            $filePath = \Yii::getAlias($yiiAliasUploadsRoot).$prefix.$id_file_name;
            if (!is_dir(dirname($filePath))) {
                FileHelper::createDirectory(dirname($filePath));
            }
            file_put_contents($filePath, $content);
            $file = new Files();
            $file->filename = $id_file_name;
            $file->extension = $ext;
            $file->mime_type = \Yii::$app->wMimeTypes->getMimeType($ext);
            $file->relation_path = $prefix;
            $file->yii_alias_uploads_root = $yiiAliasUploadsRoot;
            $file->size = $size;
            $file->type = $file::TYPE_1;
            $file->component_id = 'localFs';
            if (!$file->save()) {
                throw new \Exception("文件保存失败：".Model::getModelError($file));
            }
        }
        return $file->abUrl;
    }
}
